package com.maozzi.service.user.domain.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maozzi.common.util.ParamCheckUtil;
import com.maozzi.service.user.domain.UserDomainService;
import com.maozzi.service.user.repository.mapper.UserMapper;
import com.maozzi.service.user.repository.model.User;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * user domain service
 *
 * @author maozi
 */
@Service
public class UserDomainServiceImpl extends ServiceImpl<UserMapper, User> implements UserDomainService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getUser(Long userId) {
        User user = userMapper.selectById(userId);
        if (Objects.isNull(user)) {
            return null;
        }

        user.setPassword(null);
        return user;
    }

    @Override
    public User getUser(String name, String password) {
        return userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getName, name)
                .eq(User::getPassword, password)
                .eq(User::getDeleted, Boolean.FALSE)
        );
    }

    @Override
    public Boolean signup(User user) {
        ParamCheckUtil.notNull(user, "参数错误");
        ParamCheckUtil.notNull(user.getName(), "名称为空");
        ParamCheckUtil.notNull(user.getPassword(), "密码为空");
        ParamCheckUtil.notNull(user.getAvatar(), "头像为空");

        user.setId(null);
        user.setDeleted(false);
        user.setCreateDate(null);
        user.setCreateBy(user.getName());
        user.setModifiedDate(null);

        this.save(user);

        return true;
    }
}
